package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

//@Component//交给容器管理
public class RedisDataPersistenceJob {
    //引入服务
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    //定时任务注解
    //corn:时间计划表达式
    @Scheduled(cron = "0/100 * * * * ?")
    public void doWork() {
        System.out.println("--------------vo持久化开始操作--------------");
        //从redis中获取所有vo对象 根据key的灵活性 通过前缀来找到vo对象 //strategy_statis_vo:*  所有攻略明细id
        List<StrategyStatisVO> listVO = strategyStatisVOService.getVOByPatten(RedisKeys.STRATEGY_STATIS_VO.join("*"));
        StrategyStatisPersistenceVO pvo = null;
        //遍历所有vo对象,然后保存到mysql中
        for (StrategyStatisVO vo : listVO) {
            pvo = new StrategyStatisPersistenceVO();
            //因为有循环引入依赖的问题,那么就新建一个vo对象,专门存放数据统计相关的持久化数据
            BeanUtil.copyProperties(pvo, vo);
            //将vo对象update mysql数据库
            strategyDetailService.updateStatisVO(pvo);
        }
        System.out.println("--------------vo持久化结束操作--------------");
    }
}
